package _case

import "fmt"

/**
运算符优先级
1.一元运算符优先级最高, 例如: ++ -- ! ^
2.二元运算符优先级从高到低
*	/	%	<<	>>	&	&^
+	-	|	^
==	!=	<	<=	>	>=
&&
||
*/

// ArithmeticCase 算术运算符
func ArithmeticCase() {
	var a = 21
	var b = 10
	var c int

	c = a + b
	fmt.Printf("a(%d) + b(%d) 的值为 %d \n", a, b, c)
	c = a - b
	fmt.Printf("a(%d) - b(%d) 的值为 %d \n", a, b, c)
	c = a * b
	fmt.Printf("a(%d) * b(%d) 的值为 %d \n", a, b, c)
	c = a / b
	fmt.Printf("a(%d) / b(%d) 的值为 %d \n", a, b, c)
	c = a % b
	fmt.Printf("a(%d) %% b(%d) 的值为 %d \n", a, b, c)
	a++
	fmt.Printf("a(%d)++ 的值为 %d \n", a-1, a)
	a--
	fmt.Printf("a(%d)-- 的值为 %d \n", a+1, a)
}

// RelationCase 关系运算符, 格式化字符串: https://www.runoob.com/go/go-fmt-printf.html
func RelationCase() {
	var a = 21
	var b = 10
	fmt.Printf("a(%d) == b(%d) 的结果为(%t) \n", a, b, a == b)
	fmt.Printf("a(%d) > b(%d) 的结果为(%t) \n", a, b, a > b)
	fmt.Printf("a(%d) < b(%d) 的结果为(%t) \n", a, b, a < b)
	fmt.Printf("a(%d) >= b(%d) 的结果为(%t) \n", a, b, a >= b)
	fmt.Printf("a(%d) <= b(%d) 的结果为(%t) \n", a, b, a <= b)
	fmt.Printf("a(%d) != b(%d) 的结果为(%t) \n", a, b, a != b)
}

// LogicCase 逻辑运算符
func LogicCase() {
	var a = true
	var b = false
	fmt.Printf("a(%t) && b(%t) 结果为 (%t) \n", a, b, a && b)
	fmt.Printf("a(%t) || b(%t) 结果为 (%t) \n", a, b, a || b)
	fmt.Printf("!(a(%t) && b(%t)) 结果为 (%t) \n", a, b, !(a && b))
}

// BitCase 位运算符
func BitCase() {
	var a uint8 = 60
	var b uint8 = 13
	var c uint8 = 0
	fmt.Printf("%08b\n", a)
	fmt.Printf("%08b\n", b)
	fmt.Printf("%08b\n", c)
	fmt.Println()

	c = a & b
	fmt.Println("a & b : 与(有0则0,全1则1)")
	fmt.Printf("%08b\n", a)
	fmt.Printf("%08b\n", b)
	fmt.Printf("%08b\n", c)
	fmt.Println()

	c = a | b
	fmt.Println("a | b : 或(有1则1,全0则0)")
	fmt.Printf("%08b\n", a)
	fmt.Printf("%08b\n", b)
	fmt.Printf("%08b\n", c)
	fmt.Println()

	c = a ^ b
	fmt.Println("a ^ b : 异或(相同为0,不同为1)")
	fmt.Printf("%08b\n", a)
	fmt.Printf("%08b\n", b)
	fmt.Printf("%08b\n", c)
	fmt.Println()

	c = a << 2
	fmt.Println("a << 2 : 左移")
	fmt.Printf("%08b\n", a)
	fmt.Printf("%08b\n", c)
	fmt.Println()

	c = a >> 2
	fmt.Println("a >> 2 : 右移")
	fmt.Printf("%08b\n", a)
	fmt.Printf("%08b\n", c)
	fmt.Println()

	c = ^a
	fmt.Println("^a : 取反")
	fmt.Printf("%08b\n", a)
	fmt.Printf("%08b\n", c)
	fmt.Println()

	c = a &^ b
	fmt.Println("a &^ b :")
	fmt.Printf("%08b\n", a)
	fmt.Printf("%08b\n", b)
	fmt.Printf("%08b\n", c)
	fmt.Println()

	c = a & ^b
	fmt.Println("a & ^b :")
	fmt.Printf("%08b\n", a)
	fmt.Printf("%08b\n", b)
	fmt.Printf("%08b\n", c)
}

// AssignmentCase 赋值运算符
func AssignmentCase() {
	var a = 21
	var c int
	c = a
	fmt.Println("c = a, c的值为: ", c)
	c += a
	fmt.Println("c += a, c的值为: ", c)
	c -= a
	fmt.Println("c -= a, c的值为: ", c)
	c *= a
	fmt.Println("c *= a, c的值为: ", c)
	c /= a
	fmt.Println("c /= a, c的值为: ", c)
	c %= a
	fmt.Println("c %= a, c的值为: ", c)

	var b uint8 = 60
	fmt.Printf("b 值为 %d, 二进制表示: %08b\n", b, b)
	b <<= 2
	fmt.Printf("b <<= 2, b 值为 %d, 二进制表示: %08b\n", b, b)
	b >>= 2
	fmt.Printf("b >>= 2, b 值为 %d, 二进制表示: %08b\n", b, b)
	b &= 2
	fmt.Printf("b &= 2, b 值为 %d, 二进制表示: %08b\n", b, b)
	b ^= 2
	fmt.Printf("b ^= 2, b 值为 %d, 二进制表示: %08b\n", b, b)
	b |= 2
	fmt.Printf("b |= 2, b 值为 %d, 二进制表示: %08b\n", b, b)
}
